package com.wh.testCycliBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 集齐七龙珠召唤神龙
 */
public class CycliBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("集齐7颗龙珠召唤神龙");
        });
        for(int i=0;i<7;i++){
            //lambda能操作到i么？
            final int tem=i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集了第"+tem+"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"---end");
            },i+":线程").start();

        }
        System.out.println("主线程结束");
    }
}
